using System;
using FishFillets.GameComponents.Helpers;
using FishFillets.Model;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace FishFillets.GameComponents
{
    public class LevelRefComponent : DrawableGameComponent
    {
        public SpriteBatch SpriteBatch;

        private readonly ContentManager contentManager;
        private readonly AssetHelper contentAltManager;
        private readonly LevelRef level;
        private readonly LevelRef parent;

        private Texture2D nfar;
        private Texture2D n0;
        private Texture2D n1;
        private Texture2D n2;
        private Texture2D n3;
        private Texture2D n4;
        private int leftFar, topFar, leftN, topN;

        public LevelRefComponent(Game game, ContentManager contentManager, AssetHelper contentAltManager,
            LevelRef level, LevelRef parent) 
            : base(game)
        {
            this.contentManager = contentManager;
            this.contentAltManager = contentAltManager;
            this.level = level;
            this.parent = parent;
        }

        protected override void LoadContent()
        {
            nfar = contentAltManager.LoadTextureStream("images/menu/n_far");
            n0 = contentAltManager.LoadTextureStream("images/menu/n0");
            n1 = contentAltManager.LoadTextureStream("images/menu/n1");
            n2 = contentAltManager.LoadTextureStream("images/menu/n2");
            n3 = contentAltManager.LoadTextureStream("images/menu/n3");
            n4 = contentAltManager.LoadTextureStream("images/menu/n4");
            leftFar = nfar.Width / 2;
            topFar = nfar.Height / 2;
            leftN = n0.Width / 2;
            topN = n0.Height / 2;
        }

        protected override void UnloadContent()
        {
            nfar.Dispose();
            n0.Dispose();
            n1.Dispose();
            n2.Dispose();
            n3.Dispose();
            n4.Dispose();
        }

        public override void Draw(GameTime gameTime)
        {
            // draw the button, if there is any
            if (level.Solved)
            {
                // gold solved button
                SpriteBatch.Draw(n0, new Rectangle(level.Position.X - leftN, level.Position.Y - topN, n0.Width, n0.Height), Color.White);
            }
            else if (parent == null || parent.Solved)
            {
                Texture2D t;
                // comppute button phase
                int phase = (gameTime.TotalGameTime.Milliseconds % 1000) / 100;
                if (phase > 4) {
                    phase--;
                }
                if (phase > 7) {
                    phase--;
                }
                if (phase >= 4) {
                    phase = 7 - phase;
                }

                switch (phase)
                {
                    case 0:
                        t = n1;
                        break;
                    case 1:
                        t = n2;
                        break;
                    case 2:
                        t = n3;
                        break;
                    case 3:
                        t = n4;
                        break;
                    default:
                        throw new ArgumentOutOfRangeException("gameTime");
                }
                // blue ready button
                SpriteBatch.Draw(t, new Rectangle(level.Position.X - leftN, level.Position.Y - topN, t.Width, t.Height), Color.White);
            }
        }
    }
}
